'''
作者: 杨萍
介绍:实验管理-实验室预约审批模块查询接口测试
'''
import unittest
from jsonpath import jsonpath
from parameterized.parameterized import parameterized

from utils.reader import Reader
from utils.session import Session


class QueryTest(unittest.TestCase):
    """
    介绍:查询接口测试
    作者: 杨萍
    """
    session = None
    url = "http://120.53.243.26:8009/api/laboratoryReserve/findPage"
    header = None

    @classmethod
    def setUpClass(cls) -> None:
        """
        测试类级别的脚手架方法，在测试类开始时执行，运行在此类测试方法调用之前
        """
        cls.session = Session.get_session()
        cls.header = Session.get_header()

    @classmethod
    def tearDownClass(cls) -> None:
        """
        测试类级别的脚手架方法，在测试类开始时执行，运行在此类测试方法调用之前
        """
        cls.session.close()


    @parameterized.expand(Reader.read_excel('query_data.xlsx', min_row=1, max_row=34, min_col=1, max_col=6))
    def test_query(self, applicant, experimentName, laboratory, status, expected_code, expected_Msg):
        """
        实验室预约审批模块 查询接口
        """
        data = {"pageNumber": 0, "pageSize": 10, "pageOrder": [],
                "searchDto":{"includeSea": "", "deptSysCodeSea": "", "deptCasCodeSea": "","deptNameSea": "", "fromMenu": "",
                            "searchExample": {"applicant": applicant, "dateRange":["2022-01-13T16:00:00.000Z", "2022-11-04T16:00:00.000Z"], "experimentName":experimentName,
                                              "laboratory": laboratory, "status":status}
                            }
                }

        # 发送请求
        resp = self.session.post(url=self.url, json=data, headers=self.header)

        # 断言状态码为200
        self.assertEqual(200, resp.status_code)

        # 根据响应提取出实际数据
        body = resp.json()

        # 实际响应中的resCode,resMsg, id
        real_resCode = jsonpath(body, "$..resCode")
        real_resMsg = jsonpath(body, "$..resMsg")
        content = jsonpath(body, "$..content")

        # 断言响应信息中的content值不为空列表时，resCode与预期值一致,resMsg为“OK”；content值为空列表时，其长度为0
        if len(content) != 0:
            self.assertEqual(expected_code, real_resCode[0])
            self.assertEqual(expected_Msg, real_resMsg[0])
        else:
            self.assertEqual(0, len(content))





